<template>
  <div class="reception-visitor">
    <div class="header">
      <h2>游客咨询管理</h2>
      <p>管理未登录游客的咨询消息，提供在线解答服务</p>
    </div>

    <el-card class="main-card">
      <!-- 咨询列表 -->
      <div class="consultation-list">
        <div class="list-header">
          <h3>游客咨询列表</h3>
          <el-button type="primary" @click="refreshList">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>

        <el-table
          :data="consultationList"
          v-loading="loading"
          style="width: 100%"
          @row-click="handleRowClick"
        >
          <el-table-column prop="visitorName" label="游客姓名" width="120" />
          <el-table-column prop="phone" label="联系电话" width="130" />
          <el-table-column prop="content" label="咨询内容" min-width="200" show-overflow-tooltip />
          <el-table-column prop="createTime" label="咨询时间" width="160">
            <template #default="{ row }">
              {{ formatTime(row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template #default="{ row }">
              <el-tag :type="row.status === '已回复' ? 'success' : 'warning'">
                {{ row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                type="primary"
                link
                @click.stop="handleReply(row)"
                :disabled="row.status === '已回复'"
              >
                回复
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.current"
            v-model:page-size="pagination.size"
            :total="pagination.total"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>

      <!-- 回复对话框 -->
      <el-dialog
        v-model="replyDialogVisible"
        title="回复游客咨询"
        width="600px"
        :before-close="handleCloseReply"
      >
        <el-form :model="replyForm" label-width="80px">
          <el-form-item label="游客姓名">
            <el-input v-model="currentConsultation.visitorName" disabled />
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="currentConsultation.phone" disabled />
          </el-form-item>
          <el-form-item label="咨询内容">
            <el-input
              v-model="currentConsultation.content"
              type="textarea"
              :rows="3"
              disabled
            />
          </el-form-item>
          <el-form-item label="回复内容" required>
            <el-input
              v-model="replyForm.content"
              type="textarea"
              :rows="4"
              placeholder="请输入回复内容"
              maxlength="500"
              show-word-limit
            />
          </el-form-item>
        </el-form>
        
        <template #footer>
          <el-button @click="handleCloseReply">取消</el-button>
          <el-button type="primary" @click="submitReply" :loading="replying">
            发送回复
          </el-button>
        </template>
      </el-dialog>

      <!-- 咨询详情对话框 -->
      <el-dialog
        v-model="detailDialogVisible"
        title="咨询详情"
        width="500px"
      >
        <div class="consultation-detail">
          <div class="detail-item">
            <label>游客姓名：</label>
            <span>{{ currentConsultation.visitorName }}</span>
          </div>
          <div class="detail-item">
            <label>联系电话：</label>
            <span>{{ currentConsultation.phone }}</span>
          </div>
          <div class="detail-item">
            <label>咨询时间：</label>
            <span>{{ formatTime(currentConsultation.createTime) }}</span>
          </div>
          <div class="detail-item">
            <label>咨询内容：</label>
            <div class="content-box">{{ currentConsultation.content }}</div>
          </div>
          <div v-if="currentConsultation.replyContent" class="detail-item">
            <label>回复内容：</label>
            <div class="content-box reply-content">{{ currentConsultation.replyContent }}</div>
          </div>
          <div v-if="currentConsultation.replyTime" class="detail-item">
            <label>回复时间：</label>
            <span>{{ formatTime(currentConsultation.replyTime) }}</span>
          </div>
        </div>
        
        <template #footer>
          <el-button type="primary" @click="detailDialogVisible = false">关闭</el-button>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import { getVisitorConsultations, replyVisitorConsultation } from '@/api/reception'

// 响应式数据
const loading = ref(false)
const replying = ref(false)
const replyDialogVisible = ref(false)
const detailDialogVisible = ref(false)

const consultationList = ref([])
const currentConsultation = ref({})
const replyForm = reactive({
  content: ''
})

const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  return new Date(time).toLocaleString('zh-CN')
}

// 加载咨询列表
const loadConsultations = async () => {
  loading.value = true
  try {
    const response = await getVisitorConsultations({
      page: pagination.current,
      size: pagination.size
    })
    consultationList.value = response.data.records || []
    pagination.total = response.data.total || 0
  } catch (error) {
    ElMessage.error('加载咨询列表失败：' + error.message)
  } finally {
    loading.value = false
  }
}

// 刷新列表
const refreshList = () => {
  pagination.current = 1
  loadConsultations()
}

// 处理行点击
const handleRowClick = (row) => {
  currentConsultation.value = { ...row }
  detailDialogVisible.value = true
}

// 处理回复
const handleReply = (row) => {
  currentConsultation.value = { ...row }
  replyForm.content = ''
  replyDialogVisible.value = true
}

// 关闭回复对话框
const handleCloseReply = () => {
  replyDialogVisible.value = false
  replyForm.content = ''
}

// 提交回复
const submitReply = async () => {
  if (!replyForm.content.trim()) {
    ElMessage.warning('请输入回复内容')
    return
  }

  replying.value = true
  try {
    await replyVisitorConsultation({
      consultationId: currentConsultation.value.id,
      replyContent: replyForm.content.trim()
    })
    
    ElMessage.success('回复成功')
    replyDialogVisible.value = false
    replyForm.content = ''
    loadConsultations()
  } catch (error) {
    ElMessage.error('回复失败：' + error.message)
  } finally {
    replying.value = false
  }
}

// 分页处理
const handleSizeChange = (size) => {
  pagination.size = size
  pagination.current = 1
  loadConsultations()
}

const handleCurrentChange = (current) => {
  pagination.current = current
  loadConsultations()
}

// 生命周期
onMounted(() => {
  loadConsultations()
})
</script>

<style scoped>
.reception-visitor {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
  color: #303133;
}

.header p {
  margin: 8px 0 0;
  color: #606266;
  font-size: 14px;
}

.main-card {
  min-height: 500px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.list-header h3 {
  margin: 0;
  color: #303133;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.consultation-detail {
  padding: 10px 0;
}

.detail-item {
  margin-bottom: 16px;
  display: flex;
  align-items: flex-start;
}

.detail-item label {
  width: 80px;
  font-weight: bold;
  color: #606266;
  flex-shrink: 0;
}

.detail-item span {
  color: #303133;
}

.content-box {
  background: #f5f7fa;
  padding: 12px;
  border-radius: 4px;
  border: 1px solid #e4e7ed;
  color: #303133;
  line-height: 1.5;
}

.reply-content {
  background: #f0f9ff;
  border-color: #bae6fd;
}

:deep(.el-table__row) {
  cursor: pointer;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa;
}
</style>
